﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

namespace Vmago.Lib
{
    public class GoogleAPI
    {
        public struct AddreassLongLat
        {
            public string Address;
           public string Long;
           public string Lat;
        }
        public AddreassLongLat getLongLat(string address)
        {
                
               
               // 查詢經緯度
               
               string output = "csv";
               
               string key = "ABQIAAAAXDq__hWKi9eMCwnn7LrMCxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSnSVp_Xlsd4Ph5iyMua7PE5E0x_A";
               
               string url = string.Format("http://maps.google.com/maps/geo?q={0}&output={1}&key={2}", address, output, key);
               
                
               
               WebClient wc = new WebClient();
               
                
               
               // 讀取結果
               
               Stream s = wc.OpenRead(url);
               
               StreamReader sr = new StreamReader(s, Encoding.UTF8);
               
               string result = sr.ReadToEnd();
               
                
               
               // 解析 200,8,25.033408,121.564099  (HTTP status code, accuracy, latitude, longitude)
               
               string[] tmpArray = result.Split(',');

               AddreassLongLat a = new AddreassLongLat();
               a.Lat = tmpArray[2];
               a.Long = tmpArray[3];
               a.Address = address;

               return a;                                             
                                              
        }
    }
}
